/*
 * Copyright 2010 Marcin Blazejewski
 *
 * This file is part of Nofate.
 *
 * Nofate is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Nofate is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nofate. If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "action.hpp"
#include "map.hpp"

#include <stdlib.h>
#include <string.h>
#include <iostream>

int Action::tick() {
    //Thing *cre = a->cre;
    //LL *i;

    timer -= 1;

    if (timer == 0) {
        execute();
        return 1;
    }

    return 0;
    /*
    if ((a->timer <= 0) && (a->cre != NULL)) {
        cre->cre.actions = ll_remove_obj(cre->cre.actions, a);
        

        // add first action on queue (*last* in the list, lists work like stacks)
        for (i = cre->cre.actions; i != NULL; i = i->next) {
            if (i->next == NULL) break;
        }
        if (i != NULL) {
            am_add_action(a->area, (Action*)i->obj);
        }

        free(a);   WRONG!!
    }
    */
}

/* --------------------------------------------------------------------------- */

MoveAction::MoveAction(char _dx, char _dy) {
    dx = _dx;
    dy = _dy;
    //std::cout << "constructor dx=" << (int)dx << ", dy=" << (int)dy << std::endl;
}

int MoveAction::start() {
    int tx = cre->x + dx;
    int ty = cre->y + dy;


    if ( (tx < 0) || (ty < 0) || (tx >= area->width) || (ty >= area->height) )
        return -1;

    if ((area->cells[tx][ty].alpha + cre->alpha) > 100)
        return -2;

    
    timer = 5;
    //std::cout << "start dx=" << (int)dx << ", dy=" << (int)dy << std::endl;
    return 0;
}

int MoveAction::execute() {
    //std::cout << "execute dx=" << (int)dx << ", dy=" << (int)dy << std::endl;
    area->move_thing(cre, cre->x + dx, cre->y + dy);

    return 0;
}

int AttributeAction::start() {
    return 0;
}

int AttributeAction::execute() {
    return 0;
}

